<?php

global $HTMLEDITOR, $EDHEIGHT, $SITEURL;
$mld = MLDnewsletter::getInstance();
$letterid = ($_GET['editletter'] == '') ? '<new>' : $_GET['editletter'];
$xml = $mld->getXmlData('newsletters');

if (isset($_POST['saveletter']) && ($letterid != '<new>')) {
    remove_letter($letterid);
}

if (isset($_POST['saveletter'])) {
    $letterid = (string) uniqid();
    $thisletter = $xml->addChild('letter');
    $newletter = $thisletter->addChild('title');
    $newletter->addCData($_POST['newsletter']['title']);
    $thisletter->addChild('id', $letterid);
    $thisletter->addChild('send', $_POST['newsletter']['alreadysend']);
    $newletter = $thisletter->addChild('content');
    $newletter->addCdata(htmlentities($_POST['newsletter']['content'], ENT_QUOTES, 'UTF-8'));
    if (XMLsave($xml, $mld->files['newsletters'])) {
        display_message('updated', i18n_r('mld-newsletter/LETTERSAVED'));
    } else {
        display_message('error', i18n_r('mldnewsletter/LETTERSAVEFAIL'));
    }
}

$xml = $mld->getXmlData('newsletters');
$lettertitle = '';
$lettersend = '';
$lettercontent = '';

if ($letterid != '<new>') {
    foreach ($xml as $letter) {
        if (((string) $letter->id) == $letterid) {
            $lettertitle = $letter->title;
            $lettersend = $letter->send;
            //$lettercontent = htmldecode($letter->content);
            $lettercontent = stripslashes(htmldecode($letter->content));
            break;
        }
    }
} else {
    $lettertitle = "<new>";
}
$alreadysend = ($letterid == '<new>') ? 'false' : $lettersend;

//show editform

if ($HTMLEDITOR != '') {
    if (defined('GSEDITORHEIGHT')) {
        $EDHEIGHT = GSEDITORHEIGHT . 'px';
    } else {
        $EDHEIGHT = '500px';
    }
    if (defined('GSEDITORLANG')) {
        $EDLANG = GSEDITORLANG;
    } else {
        $EDLANG = 'en';
    }
}
?>
<h3><?php i18n('mld-newsletter/EDITLETTER'); ?></h3>
    <div class="edit-nav" ><p><a href="<?php echo $_SERVER['SCRIPT_NAME'] . '?id=mld-newsletter&viewletter=' . $letterid; ?>"><?php i18n('mld-newsletter/VIEWLETTER'); ?></a></p></div>
    <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post" class="mldeditform">
        <p><label class="mldlabel"><?php i18n('mld-newsletter/LETTERTITLE'); ?>:</label>
            <input type="text" class="text" name="newsletter[title]" value="<?php echo $lettertitle; ?>" /></p>
        <input type="hidden" name="newsletter[alreadysend]" value="<?php echo $alreadysend; ?>" />
                <!--<p><label class="mldlabel"><?php i18n('mld-newsletter/ALREADYSEND'); ?>: <?php echo $lettersend; ?></label></p>-->
        <p><textarea id="newsletter-content" name="newsletter[content]"><?php echo $lettercontent; ?></textarea></p>
        <p><input type="submit" class="submit" value="<?php i18n('mld-newsletter/SAVELETTER'); ?>" id="saveletter" name="saveletter" /></p>
    </form>
    <script type="text/javascript" src="template/js/ckeditor/ckeditor.js"></script>
    <script type="text/javascript">
        var editor = CKEDITOR.replace( 'newsletter-content', {
            skin : 'getsimple',
            forcePasteAsPlainText : true,
            language : '<?php echo $EDLANG; ?>',
            defaultLanguage : '<?php echo $EDLANG; ?>',
            entities : false,
            uiColor : '#FFFFFF',
            height: '<?php echo $EDHEIGHT; ?>',
            baseHref : '<?php echo $SITEURL; ?>',
            toolbar : [['Bold','Italic','Underline','JustifyLeft','JustifyCenter','JustifyRight','Table','TextColor','Link',
                    'Unlink','Image','Font','FontSize','Source']],
            filebrowserBrowseUrl : 'filebrowser.php?type=all',
            filebrowserImageBrowseUrl : 'filebrowser.php?type=images',
            filebrowserWindowWidth : '730',
            filebrowserWindowHeight : '500'
        });
    </script>